import api from '../../api';
import { message } from 'antd';
export default {
  namespace: 'dashboard',
  state: {
    bondNameList: [],
    toDoTableData: {}, //待办列表返回的数据
    didTableData: {}, //已办列表返回的数据
  },

  effects: {
    //债券简称模糊查询
    *queryBondNames({ payload }, { call, put, select }) {
      const res = yield call(api.bondBaseInfo.queryBondName, payload);
      if (res) {
        if (res.code == 0) {
          const arr = [];
          res.data.dataSource.map((item) => {
            arr.push({
              value: item.secucode,
              title: item.tradingCode + '-' + item.secuabbr,
              //资管-债券信息-CAMS表查询字段
              tradingCode: item.tradingCode,
              exchangeCode: item.exchangeCode,
            });
          });
          yield put({
            type: 'setState',
            payload: {
              bondNameList: arr,
            },
          });
        } else {
          message.error(res.msg || '债券简称查询失败!');
        }
      } else {
        message.error('债券简称查询失败!');
      }
    },
    //获取待办消息列表
    *getFlowToDoList({ payload, callback }, { call, put, select }) {
      const res = yield call(api.taskList.getFlowToDoList, payload);
      if (res) {
        if (res.code == 0) {
          yield put({
            type: 'setState',
            payload: {
              toDoTableData: res.data || {},
            },
          });
          if (callback) callback(res.data || {});
        } else {
          yield put({
            type: 'setState',
            payload: {
              toDoTableData: {},
            },
          });
          if (callback) callback();
          message.error(res.msg || '待办列表查询失败!');
        }
      } else {
        yield put({
          type: 'setState',
          payload: {
            toDoTableData: {},
          },
        });
        message.error('待办列表查询失败!');
      }
    },
    //获取已办消息列表
    *getFlowDidList({ payload, callback }, { call, put, select }) {
      const res = yield call(api.taskList.getFlowDidList, payload);
      if (res) {
        if (res.code == 0) {
          callback(res.data || {});
        } else {
          callback();
          message.error(res.msg || '已办列表失败!');
        }
      } else {
        callback();
        message.error('已办列表失败!');
      }
    },

    // 删除待办任务
    *deleteData({ payload, callback }, { call, put, select }) {
      const res = yield call(api.taskList.deleteData, payload);
      if (res) {
        if (res.code == 0) {
          callback(true);
          message.success(res.msg || '删除成功!');
        } else {
          callback();
          message.error(res.msg || '删除失败!');
        }
      } else {
        callback();
        message.error('删除失败!');
      }
    },
  },
  reducers: {
    setState(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
